package org.emavaj.common;

import java.io.FileInputStream;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

/**
 * 
 * @author eMavaj
 *
 */
public class AudioThread extends Thread {

	private FileInputStream stream;
	private Player player;
	private boolean isLoop = false;

	/**
	 * 构造函数
	 * 
	 * @param stream
	 *            音乐流
	 */
	public AudioThread(FileInputStream stream, boolean isLoop) {
		this.stream = stream;
		this.isLoop = isLoop;
	}

	@Override
	public void run() {
		try {
			player = new Player(stream);
			do {
				player.play();
			} while (isLoop);
		} catch (JavaLayerException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 停止音乐
	 */
	public void stopPlay() {
		player.close();
		// 跳出循环
		isLoop = false;
	}
}
